package com.facedemo.bean;

import android.graphics.Rect;

import com.facedemo.utils.LogUtils;

import net.gdface.sdk.CodeInfo;


public class FaceTraceInfo {
    private String id;

    private long createTime = 0;
    private int updateNum = 0;

    private CodeInfo codeInfo;//人脸检测结果
    private Rect rect;
    private int bitmapW;
    private int bitmapH;

    public int liveDetectedFailedNum = 0;//活体检测失败次数
    public LiveStatus liveStatus = LiveStatus.UNKNOWN;// 活体检测 活体状态

    private int recognizeStatus = 0;//当前状态
    public static final int STATUS_WAIT = 0;//未进行人脸识别
    public static final int STATUS_FACE_RECOGNIZE = 1;//正在进行人脸识别
    public static final int STATUS_FACE_RECOGNIZE_SUCCESS = 2;//人脸识别成功

    private int recognizeNum = 0;//人脸识别次数
    private String resultId = null;//识别成功结果:人员ID
    private String resultName = null;//识别成功结果:姓名

    private long lastTraceTime = 0;//最后一次更新人脸追踪信息的时间

    /**
     * @param codeInfo 人脸检测结果
     * @param bitmapW 图像宽度
     * @param bitmapH 图像高度
     * @param traceTimeTag 当前时间戳
     * */
    public FaceTraceInfo(CodeInfo codeInfo, int bitmapW, int bitmapH, long traceTimeTag) {
        createTime =System.currentTimeMillis();
        updateNum ++;
        id = ""+System.currentTimeMillis();

        this.codeInfo = codeInfo;
        this.rect = getRectFromCodeInfo(codeInfo);
        this.bitmapW = bitmapW;
        this.bitmapH = bitmapH;
        this.lastTraceTime = traceTimeTag;
    }

    /**
     * 人脸追踪信息更新
     * @param codeInfo 新的人脸检测结果
     * @param newRect 人脸框
     * @param traceTimeTag 当前时间戳
     * */
    public void updateTraceInfo(CodeInfo codeInfo, Rect newRect,long traceTimeTag) {
        updateNum ++;
        this.codeInfo = codeInfo;
        this.rect = newRect;
        this.lastTraceTime = traceTimeTag;
    }

    private Rect getRectFromCodeInfo(CodeInfo codeInfo) {
        if(codeInfo == null) return null;
        Rect rect = new Rect(codeInfo.getPos().getLeft(),
                codeInfo.getPos().getTop(),
                codeInfo.getPos().getLeft() + codeInfo.getPos().getWidth(),
                codeInfo.getPos().getTop() + codeInfo.getPos().getHeight());
        return rect;
    }

    public String getId() {
        return id;
    }

    /**
     * 标记人脸识别成功
     * @param personId 识别成功结果:人员ID
     * @param personName 识别成功结果:姓名
     * */
    public void setDetectionSuccess(String personId,String personName) {
        recognizeNum++;
        setRecognizeStatus(STATUS_FACE_RECOGNIZE_SUCCESS);
        this.resultId = personId;
        this.resultName = personName;

        LogUtils.printD("FA==》成功，过程用时："+(System.currentTimeMillis() - createTime)+",经过了："+updateNum+"帧，识别了："+recognizeNum+"次");
    }

    /**
     * 标记人脸识别失败
     * */
    public void setDetectionFailed() {
        recognizeNum++;
        setRecognizeStatus(STATUS_WAIT);
    }

    public int getRecognizeStatus() {
        return recognizeStatus;
    }

    public void setRecognizeStatus(int recognizeStatus) {
        this.recognizeStatus = recognizeStatus;
    }

    public Rect getRect() {
        return rect;
    }

    public int getBitmapW() {
        return bitmapW;
    }

    public int getBitmapH() {
        return bitmapH;
    }

    public long getLastTraceTime() {
        return lastTraceTime;
    }

    public int getRecognizeNum() {
        return recognizeNum;
    }

    public CodeInfo getCodeInfo() {
        return codeInfo;
    }

    public String getResultId() {
        return resultId;
    }

    public String getResultName() {
        return resultName;
    }

    public int getUpdateNum() {
        return updateNum;
    }
}
